package xjtu.se.articlecommunity.common.SSE.dao;

import org.apache.ibatis.annotations.*;
import xjtu.se.articlecommunity.common.SSE.dto.Mess;
import xjtu.se.articlecommunity.common.SSE.dto.MessShow;

import java.util.List;

@Mapper
public interface MessDao {

    @Insert("insert into t_message(m_id, u_receive_id, u_send_id, m_type, a_id, c_id) " +
            "values (#{m_id}, #{u_receive_id}, #{u_send_id}, #{m_type}, #{a_id}, #{c_id})")
    void insert(Mess mess);

    @Select("select u_id from t_article where a_id = #{id}")
    String getArticleAuthor(@Param("id") String a_id);

    @Select("select a_id from t_comment where c_id = #{id}")
    String getAByC(@Param("id") String id);

    @Select("select u_id from t_comment where c_id = #{id}")
    String getCommentAuthor(@Param("id") String id);

    @Select("select u_id from t_comment where c_id = " +
            "( select fc_id from t_comment where c_id = #{id} )")
    String getReplyTarget(@Param("id") String id);

    @Select("select t_message.a_id, a_title, m_date, " +
            "ifchecked as ifChecked, m_type as type, m_id \n" +
            "from t_message, t_article \n" +
            "where t_message.a_id = t_article.a_id \n" +
            "and u_receive_id = #{id} \n" +
            "and m_type in (5, 6) \n" +
            "order by m_date DESC")
    List<MessShow> getApproveList(@Param("id") String u_id,
                                  @Param("type1") String type1,
                                  @Param("type2") String type2);

    @Select("select t_user.u_id, t_user.u_name, m_date, m_id, " +
            "t_message.a_id, a_title, m_type as type, u_avatar, " +
            "t_message.ifchecked as ifChecked " +
            "from t_message, t_user, t_article " +
            "where t_message.u_send_id = t_user.u_id " +
            "and t_message.a_id = t_article.a_id " +
            "and m_type in (#{type1}, #{type2}) " +
            "and u_receive_id = #{id} " +
            "order by m_date DESC")
    List<MessShow> getLikesList(@Param("id") String u_id,
                                @Param("type1") String type1,
                                @Param("type2") String type2);

    @Select("select m_id, t_user.u_id, t_user.u_name, u_avatar, m_date, " +
            "t_message.a_id, a_title, c_detail, m_type as type, " +
            "t_message.ifchecked as ifChecked " +
            "from t_message, t_user, t_comment, t_article " +
            "where t_message.u_send_id = t_user.u_id " +
            "and t_message.c_id = t_comment.c_id " +
            "and t_message.a_id = t_article.a_id " +
            "and m_type = #{type} " +
            "and u_receive_id = #{id} " +
            "order by m_date DESC")
    List<MessShow> getCommentList(@Param("id") String u_id,
                                  @Param("type") String type);

    @Update("UPDATE t_message SET ifchecked = 1 WHERE u_receive_id = #{u_id} and " +
            "m_type in (#{ messType1 }, #{ messType2 })")
    void clear(@Param("messType1") String messType1, @Param("messType2") String messType2, @Param("u_id") String u_id);
}
